//
//  GYHomeSaleDetailVC.m
//  GYDemo
//
//  Created by Mrzhang on 2018/4/8.
//  Copyright © 2018年 mac. All rights reserved.
//

//
//                .-~~~~~~~~~-._       _.-~~~~~~~~~-.
//            __.'              ~.   .~              `.__
//          .'//                  \./                  \\`.
//        .'//                     |                     \\`.
//      .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-. \\`.
//    .'//.-"                 `-.  |  .-'                 "-.\\`.
//  .'//______.============-..   \ | /   ..-============.______\\`.
//.'______________________________\|/______________________________`.
//
//

#import "GYHomeSaleDetailVC.h"
#import "Head.h"
#import "GYMyBuyProductDetailViewModel.h"
#import "GYSaleProductDetailCell.h"
#import "GYSaleDetailBtomView.h"
#import "GYCartView.h"
#import "GYCartViewModel.h"
#import "GYCartVC.h"
#import "GYHomeShopDetailVC.h"
#import "GYMakeOrderVC.h"
#import "GYNewSaleDetailVC.h"
@interface GYHomeSaleDetailVC ()<SDCycleScrollViewDelegate,UIWebViewDelegate>
{
    float titleW;
    NSInteger currentPage;
    
}
@property(nonatomic,strong)GYMyBuyProductDetailViewModel* detailModel;
@property(nonatomic,strong)GYSaleDetailBtomView* btomView;
@property(nonatomic,strong)GYCartView* cartView;
@property(nonatomic,strong)UIView* displayView;
@property(nonatomic,strong)UIWebView* imageWebView;
@end

@implementation GYHomeSaleDetailVC
#pragma mark - getter and setter
-(UIWebView *)imageWebView
{
    if (!_imageWebView) {
        _imageWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,1)];
        _imageWebView.scrollView.showsVerticalScrollIndicator = NO;
        _imageWebView.scrollView.showsHorizontalScrollIndicator = NO;
        _imageWebView.backgroundColor=[UIColor whiteColor];
        _imageWebView.scrollView.scrollEnabled = NO;
        _imageWebView.delegate = self;
        [_imageWebView setScalesPageToFit:NO];
        if (@available(iOS 11.0, *)) {
            _imageWebView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        
    }
    return _imageWebView;
}
-(UIView *)displayView
{
    if (!_displayView) {
        _displayView=[[UIView alloc] init];
        _displayView.backgroundColor=[UIColor blackColor];
        _displayView.alpha=0.3;
        _displayView.frame=CGRectMake(0, self.view.height+100, self.view.width, self.view.height);
        @WeakSelf;
        [_displayView addTapGestureRecognizerWithDelegate:self Block:^(NSInteger tag) {
            [selfp missCartView];
        }];
    }
    return _displayView;
}
-(GYCartView *)cartView
{
    if (!_cartView) {
        _cartView=[[GYCartView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT+100, self.view.width, WFCGFloatY(216))];
        _cartView.backgroundColor=[UIColor whiteColor];
        @WeakSelf;
        NSLog(@"---看看--%f",self.view.height);
        _cartView.callback = ^(NSString *count) {
            
            
            NSMutableDictionary* para=[[NSMutableDictionary alloc] initWithDictionary:@{}];
            //                [para setObject:selfp.goodsID forKey:@"goodid"];
            [para setObject:selfp.detailModel.BuyProductDetailInfoModel.name forKey:@"name"];
            [para setObject:selfp.detailModel.BuyProductDetailInfoModel.imgpath forKey:@"image"];
            [para setObject:[NSString stringWithFormat:@"%@",[selfp.detailModel.BuyProductDetailInfoModel.price stringByReplacingOccurrencesOfString:@"," withString:@""]] forKey:@"price"];
            [para setObject:@"1" forKey:@"count"];
            [para setObject:@"红色" forKey:@"color"];
            [para setObject:@"XLL" forKey:@"sizes"];
            [[GYCartViewModel shareInterface] requestAdd:para];
            

            
            [selfp missCartView];
        };
    }
    return _cartView;
}

-(GYSaleDetailBtomView *)btomView
{
    if (!_btomView) {
        
        _btomView=[[GYSaleDetailBtomView alloc] initWithFrame:CGRectMake(0, self.view.height-45-GY_Nav_height, self.view.width, 45)];
        _btomView.backgroundColor=[UIColor whiteColor];
        _btomView.userInteractionEnabled=YES;
        _btomView.passID=_passID;
        @WeakSelf;
        _btomView.btnCallBack = ^(NSInteger type) {

            if ([[GYLoginViewModel shareInterface] loginstatus])
            {
                if (type==2)
                {
                    GYCartVC* gy=[[GYCartVC alloc] init];
                    [selfp.navigationController pushViewController:gy animated:YES];
                }
                if (type==4)
                {
                    [selfp showCartView];
                }
                if (type==5)
                {
                    GYMakeOrderVC* gy=[[GYMakeOrderVC alloc] init];
                    gy.model=selfp.detailModel.BuyProductDetailInfoModel;
                    gy.infoModel=selfp.detailModel.BuyProductUserInfoModel;
                    [selfp.navigationController pushViewController:gy animated:YES];
                }
                
            }else
            {
                [[GCPresenter shareInterface] loadLogin];
            }
        };
    }
    return _btomView;
}
-(GYMyBuyProductDetailViewModel *)detailModel
{
    if (!_detailModel) {
        _detailModel=[[GYMyBuyProductDetailViewModel alloc] init];
    }
    return _detailModel;
}
#pragma mark - event
#pragma mark - method
-(void)missCartView
{
    
    _displayView.frame=CGRectMake(0, SCREEN_HEIGHT+100, self.view.width, SCREEN_HEIGHT);
    [UIView animateWithDuration:0.4 animations:^{
        _cartView.frame=CGRectMake(0, SCREEN_HEIGHT+100, self.view.width, WFCGFloatY(216));
    }];
}
-(void)showCartView
{
    _displayView.frame=CGRectMake(0, 0, self.view.width, SCREEN_HEIGHT);
    [UIView animateWithDuration:0.4 animations:^{
        _cartView.frame=CGRectMake(0, SCREEN_HEIGHT-GY_Nav_height-WFCGFloatY(216), self.view.width, WFCGFloatY(216));
    }];
}
#pragma mark - life
//-(instancetype)init
//{
//    return [GYNewSaleDetailVC new];
//}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
   // [self.btomView layoutSubviews];
}
-(void)initilization
{
    titleW=0;
    currentPage=1;
    self.navigationItem.title = @"现货详情";
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.navigationController.navigationBar.shadowImage = [[UIImage new]rt_darkenWithLevel:0.3f];
    [self setNavgationTheme:0];
    
    [self.view addSubview:self.baseTable];
   
    
    if ([[GYLoginViewModel shareInterface] roleType]==1)
    {
        [self.view addSubview:self.btomView];;
        self.baseTable.height = self.view.height -GY_Nav_height-45;
    }else
    {
        self.baseTable.height = self.view.height -GY_Nav_height;
    }
    
    
    [self.view addSubview:self.displayView];
    [self.view addSubview:self.cartView];
    [self.baseTable setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.baseTable setSeparatorColor: Main_Color_BG];
    [self.baseTable setBackgroundColor:Main_Color_BG];
    [self.baseTable setTintColor:Main_Color_BG];
    self.baseTable.showsVerticalScrollIndicator=NO;

    @WeakSelf;
    self.baseTable.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{

        [selfp reloadData];
        [selfp loadrecord];
    }];

    [self viewModelBand];
    [self reloadData];
    [self loadrecord];
}
-(void)viewModelBand
{

    @WeakSelf;
    
    GCBaseObservationModel *loginOb = [GCBaseObservationModel new];
    loginOb.observation = [GYLoginViewModel shareInterface];
    loginOb.keyPath = @"loginstatus";
    loginOb.handler = ^(NSString *keyPath) {
       [selfp.baseTable reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationNone];
    };
    [self registObservation:loginOb];
    
    
    GCBaseObservationModel *observation = [GCBaseObservationModel new];
    observation.observation = self.detailModel;
    observation.keyPath = @"BuyProductDetailInfoModel";
    observation.handler = ^(NSString *keyPath) {
        _cartView.inforModel=self.detailModel.BuyProductDetailInfoModel;
        [_cartView layoutSubviews];
        dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            [_imageWebView loadHTMLString:selfp.detailModel.BuyProductDetailInfoModel.descriptionField baseURL:nil];
            
        });
        [selfp.baseTable reloadData];

    };
    [self registObservation:observation];
    
    
    GCBaseObservationModel *observation1 = [GCBaseObservationModel new];
    observation1.observation = self.detailModel;
    observation1.keyPath = @"BuyProductUserInfoModel";
    observation1.handler = ^(NSString *keyPath) {
       //刷新section 1
        [selfp.baseTable reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationNone];
    };
    [self registObservation:observation1];
    
    
    GCBaseObservationModel *observation2 = [GCBaseObservationModel new];
    observation2.observation = self.detailModel;
    observation2.keyPath = @"protypeArr";
    observation2.handler = ^(NSString *keyPath) {
        //刷新section 2
        [selfp.baseTable reloadData];
    };
    [self registObservation:observation2];
    
    GCBaseObservationModel *observation3 = [GCBaseObservationModel new];
    observation3.observation = self.detailModel;
    observation3.keyPath = @"RecordArr";
    observation3.handler = ^(NSString *keyPath) {
        [selfp.baseTable reloadSections:[NSIndexSet indexSetWithIndex:3] withRowAnimation:UITableViewRowAnimationNone];
    };
    [self registObservation:observation3];
    
    
    GCBaseObservationModel *observation5 = [GCBaseObservationModel new];
    observation5.observation = [GYCartViewModel shareInterface];
    observation5.keyPath = @"datas";
    observation5.handler = ^(NSString *keyPath) {
        [selfp.btomView layoutSubviews];
    };
    [self registObservation:observation5];
}
-(void)reloadData
{
    NSMutableDictionary* dic=[[NSMutableDictionary alloc] init];
    dic[@"goodsid"]=[NSString stringWithFormat:@"%@",_passID];
   [self.detailModel requestInfo:dic];
}
-(void)loadrecord
{
    NSMutableDictionary* dic=[[NSMutableDictionary alloc] init];
    dic[@"proid"]=[NSString stringWithFormat:@"%@",_passID];
    dic[@"page"]=[NSString stringWithFormat:@"%ld",currentPage];
    [self.detailModel requestRecord:dic];
}
#pragma mark - life
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor blueColor];
    
    self.view.userInteractionEnabled=YES;
    self.isGroup=YES;
    [self initilization];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */
#pragma mark - delegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section==0) {
        return 1;
    }
    if (section==1) {
        return 1;
    }
    if (section==2) {
        return 1;
    }
    return self.detailModel.RecordArr.count+1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section==0)
    {
        static NSString *CellID = @"Cellbanner";
        GYSaleProductDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID];
        if (cell == nil) {
            cell = [[GYSaleProductDetailCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellID];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        cell.inforModel=self.detailModel.BuyProductDetailInfoModel;
        cell.passArr=self.detailModel.protypeArr;
        return cell;
    }
    if (indexPath.section==2)
    {
        static NSString *CellIdentifier = @"22repeeeely";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if(cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.backgroundColor = HEXCOLOR(@"#f1f1f1");
            [cell.contentView addSubview:self.imageWebView];
        }
        return cell;
    }
    if (indexPath.section==1)
    {
        static NSString *CellID = @"Cell33ID";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellID];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
        }
        [cell.contentView removeAllSubviews];
        

        
        UILabel* nameLab=[[UILabel alloc] init];
        nameLab.frame=WFCGRectMake(16.5, 0, 100, 30);
        nameLab.textColor=Main_Color_Black;
        nameLab.font=SYSTEMFONT(14);
        [cell.contentView addSubview:nameLab];
        nameLab.text=@"革优皮草小店";

        UILabel* ValueLab=[[UILabel alloc] init];
        ValueLab.frame=WFCGRectMake(16.5+100+20, 0,335-150, 30);
        ValueLab.textColor=Main_Color_Gray;
        ValueLab.font=SYSTEMFONT(14);
        [cell.contentView addSubview:ValueLab];
        ValueLab.text=self.detailModel.BuyProductDetailInfoModel.storemobile;
        return cell;
    }
    static NSString *CellID = @"CellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

    }
    [cell.contentView removeAllSubviews];
    MyBuyProductProtypeInfoModel*model;
    if (indexPath.row!=0) {
        model=self.detailModel.RecordArr[indexPath.row-1];
    }

    NSString* unitStr=[WFFunctions WFStrCheckEmpty:model.unit]?@"":model.unit;
    float ww=(375-33)/4;
    for (int i=0; i<4; i++)
    {
        UILabel *titleLab_1 = [[UILabel alloc] init];
        titleLab_1.frame = WFCGRectMake(16.5+ww*i, 0, ww, 30);
        titleLab_1.textColor = Main_Color_Black;
        titleLab_1.font = SYSTEMFONT(12);
        titleLab_1.textAlignment = NSTextAlignmentCenter;
        [cell.contentView addSubview:titleLab_1];
        
        if (i==0) {
            titleLab_1.textAlignment=NSTextAlignmentLeft;
        }
        if (i==3) {
            titleLab_1.textAlignment=NSTextAlignmentRight;
        }
        if (indexPath.row==0)
        {
            titleLab_1.textColor = Main_Color_Gray;
            titleLab_1.text = i==0?@"购买用户":i==1?@"数量":i==2?@"成交时间":@"状态";
        }else
        {
            
            titleLab_1.text = i==0?[NSString stringWithFormat:@"%@**",![WFFunctions WFStrCheckEmpty:model.buyname]?[model.buyname substringToIndex:1]:@"*"]:i==1?[NSString stringWithFormat:@"%ld%@",model.count,unitStr]:i==2?model.transdate:model.transtatus;
        }
    }
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        return WFCGFloatY(150)+SCREEN_WIDTH;
    }
    if (indexPath.section==2) {
        return self.imageWebView.frame.size.height;
    }

    if (indexPath.section==1) {
        return WFCGFloatY(30);
    }
   
    return WFCGFloatY(30);
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    CGFloat height = [[self.imageWebView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"] floatValue];
    self.imageWebView.frame = CGRectMake(0,0, SCREEN_WIDTH, height);
    [self.baseTable reloadData];
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView* view=[UIView new];
    view.backgroundColor=[UIColor whiteColor];
    
    if (section!=0)
    {
        UIImageView* markIV=[[UIImageView alloc] init];
        markIV.frame=WFCGRectMake(355/2, 35,20,1);
        markIV.backgroundColor=[UIColor lightGrayColor];
        [view addSubview:markIV];
        
        UILabel* nameLab=[[UILabel alloc] init];
        nameLab.frame=WFCGRectMake(0, 0, 375, 36);
        nameLab.textColor=Main_Color_Black;
        nameLab.textAlignment=NSTextAlignmentCenter;
        nameLab.font=SYSTEMFONT(15);
        [view addSubview:nameLab];
        if (section==1)
        {
            nameLab.text=@"联系方式";
        }
        if (section==2)
        {
            nameLab.text=@"产品详细信息";
        }
        if (section==3)
        {
            nameLab.text=@"成交记录";
        }
    }

    return view;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView* view=[UIView new];
    view.backgroundColor=Main_Color_BG;
    return view;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section==0) {
        return .00001f;
    }
    return WFCGFloatY(36);
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 7;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==1)
    {
        if ([[GYLoginViewModel shareInterface] roleType]==1)
        {
            if ([[GYLoginViewModel shareInterface] loginstatus])
            {
                GYHomeShopDetailVC* gy=[[GYHomeShopDetailVC alloc] init];
                gy.passID=[NSString stringWithFormat:@"%@",self.detailModel.BuyProductUserInfoModel.id];
                [self.navigationController pushViewController:gy animated:YES];
            }else
            {
                [[GCPresenter shareInterface] loadLogin];
            }
        }else
        {
            [FTIndicator showErrorWithMessage:@"您还不是革优认证会员,请注册并认证~"];
        }
    }
}
@end


/*
 11111111111111111111111111111111111111001111111111111111111111111
 11111111111111111111111111111111111100011111111111111111111111111
 11111111111111111111111111111111100001111111111111111111111111111
 11111111111111111111111111111110000111111111111111111111111111111
 11111111111111111111111111111000000111111111111111111111111111111
 11111111111111111111111111100000011110001100000000000000011111111
 11111111111111111100000000000000000000000000000000011111111111111
 11111111111111110111000000000000000000000000000011111111111111111
 11111111111111111111111000000000000000000000000000000000111111111
 11111111111111111110000000000000000000000000000000111111111111111
 11111111111111111100011100000000000000000000000000000111111111111
 11111111111111100000110000000000011000000000000000000011111111111
 11111111111111000000000000000100111100000000000001100000111111111
 11111111110000000000000000001110111110000000000000111000011111111
 11111111000000000000000000011111111100000000000000011110001111111
 11111110000000011111111111111111111100000000000000001111100111111
 11111111000001111111111111111111110000000000000000001111111111111
 11111111110111111111111111111100000000000000000000000111111111111
 11111111111111110000000000000000000000000000000000000111111111111
 11111111111111111100000000000000000000000000001100000111111111111
 11111111111111000000000000000000000000000000111100000111111111111
 11111111111000000000000000000000000000000001111110000111111111111
 11111111100000000000000000000000000000001111111110000111111111111
 11111110000000000000000000000000000000111111111110000111111111111
 11111100000000000000000001110000001111111111111110001111111111111
 11111000000000000000011111111111111111111111111110011111111111111
 11110000000000000001111111111111111100111111111111111111111111111
 11100000000000000011111111111111111111100001111111111111111111111
 11100000000001000111111111111111111111111000001111111111111111111
 11000000000001100111111111111111111111111110000000111111111111111
 11000000000000111011111111111100011111000011100000001111111111111
 11000000000000011111111111111111000111110000000000000011111111111
 11000000000000000011111111111111000000000000000000000000111111111
 11001000000000000000001111111110000000000000000000000000001111111
 11100110000000000001111111110000000000000000111000000000000111111
 11110110000000000000000000000000000000000111111111110000000011111
 11111110000000000000000000000000000000001111111111111100000001111
 11111110000010000000000000000001100000000111011111111110000001111
 11111111000111110000000000000111110000000000111111111110110000111
 11111110001111111100010000000001111100000111111111111111110000111
 11111110001111111111111110000000111111100000000111111111111000111
 11111111001111111111111111111000000111111111111111111111111100011
 11111111101111111111111111111110000111111111111111111111111001111
 11111111111111111111111111111110001111111111111111111111100111111
 11111111111111111111111111111111001111111111111111111111001111111
 11111111111111111111111111111111100111111111111111111111111111111
 11111111111111111111111111111111110111111111111111111111111111111
 
 
 */
